<?php
	// Global static variable
	define("USERLANGUAGE", "userLanguage");
	define("CONTENTPATH", "../../contents/contents.xml");
	define("LANGUAGECONTROLID", "controlLanguage");
	define("LANGUAGECONTROLCSSCLASS", "controlLanguageClass");
	define("LANGUAGECONTAINERID", "controlLanguageContainer");
	define("LANGUAGESERVERSCRIPTPATH", "./scripts/php/controlLanguage.php");

	// Verify if the post have the right parameters
	if (isset($_POST[LANGUAGECONTROLID])) {
		// Verify if the session is already started
		if (!(isset($_SESSION))) {
			// Start the session
			session_start();
		}
						
		// Store session data
		$_SESSION[USERLANGUAGE] = strtolower($_POST[LANGUAGECONTROLID]);
		
		// Encode as json
		echo json_encode(GetAllContent());
	}	
	
	function CreateLanguageControl() {
		// Create the select html control
		$htmlLanguageUserControl = "<nav id=\"" . LANGUAGECONTROLID . "\" class=\"" . LANGUAGECONTROLCSSCLASS ."\">";
	
		$htmlLanguageUserControl .= "<a href=\"#\"><img id=\"pt-BR\" alt=\"pt-BR\" src=\"./css/images/brazilFlag.png\" style=\":hover {visibility : hidden;}\" /></a>";
		$htmlLanguageUserControl .= "<a href=\"#\"><img id=\"en-US\" alt=\"en-US\" src=\"./css/images/united-statesFlag.png\" /></a>";
		
		// Format the html web control
		$htmlLanguageUserControl .= "</nav>";
		
		// Format the html web control
		return $htmlLanguageUserControl;
	}
	
	function GetAllContent() {
		// Read the content xml
		$xmlContents = simplexml_load_file(CONTENTPATH);	
		
		// If the xml was read
		if ($xmlContents) {
			// Create the dictionary array
			$dictionary = array();
			
			// For all elements
			foreach ($xmlContents->xpath("//contents/item") as $xmlNode) {			
				// Add the elements to the array
				$dictionary[strval($xmlNode["id"])] = GetContent($xmlContents, strval($xmlNode["id"]));
			}
			
			// Provide the return
			return $dictionary;
		}
		else {
			// Provide the return
			return null;
		}
	}
	
	function GetContent($xmlContents, $itemID) {
		// Select the language
		$language = SelectLanguage($xmlContents);
			
		// Execute the xpath
		$contentItem = $xmlContents->xpath("//contents/item[@id=\"" . $itemID . "\"]");
		
		// Select the language
		switch ($language) {
			case "pt-br" :
				// Return the pt-BR value
				return strval($contentItem[0]->ptBR);
				
			case "en-us" :
				// Return the en-US value
				return strval($contentItem[0]->enUS);
				
			default :
				// Return the pt-BR value
				return strval($contentItem[0]->ptBR);
		}
	}
	
	function SelectLanguage($xmlContents) {
		// Verify if the session is already started
		if (!(isset($_SESSION))) {
			// Start the session
			session_start();
		}
	
		// If the user language session is set
		if (isset($_SESSION[USERLANGUAGE])) {
			// Use the session
			$language = $_SESSION[USERLANGUAGE];
		} else {
			// This is used to prevent errors
			foreach ($xmlContents->xpath("//contents/@defaultLanguage") as $attribute) {
				// Set the value from the returned tag
				$language = $attribute;
			}
		}
	
		// Return the selected language
		return strtolower($language);
	}
?>
